#!/usr/bin/python2.5
"""
Quick and Dirty Typo Blog Migration Script
(c) Russell Neches, March 31, 2008

--- BSD License Boilerplate ---
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--- BSD License Boilerplate ---

NOTE : Don't forget to set DJANGO_SETTINGS_MODULE if you want to use
this script without Django's "./manage shell" thingy.

NOTE : If you hage any 'pages' in Typo, they will be treated as
ordinary blog entries for the dates on which they were created in
Typo. If you want something else to happen to them, it's not difficult
to pick them out later.

Mapping from Typo articles to Blogmaker Entries

row        field           Entry
index      name            member
---------------------------------------------------------------------
a[0]    -> id
a[1]    -> type
a[2]    -> title        -> Entry.headline
a[3]    -> author       -> Entry.user (foreign key to User)
a[4]    -> body         -> Entry.body
a[5]    -> extended
a[6]    -> excerpt
a[7]    -> keywords     -> Entry.tags (many-to-many key to Tag)
a[8]    -> created_at   -> Entry.pub_date
a[9]    -> updated_at
a[10]   -> user_id
a[11]   -> permalink    -> Entry.slug (if exists...)
a[12]   -> guid
a[13]   -> text_filter_id
a[14]   -> whiteboard
a[15]   -> name
a[16]   -> published    -> Entry.active (note: convert int to bool)
a[17]   -> allow_pings
a[18]   -> allow_comments
a[19]   -> pubished_at
a[20]   -> state
"""

from blogmaker.blog.models import Entry, Tag
from blogmaker.comments.models import Comment
#from django.contrib.auth.models import User
#from django.contrib.contenttypes.models import ContentType
#from django.contrib.sites.models import Site
import MySQLdb
import pickle 
import string

conn = MySQLdb.connect( host    = '************',
                        user    = '************',
                        passwd  = '************',
                        db      = '************' )

cursor = conn.cursor()
cursor.execute( "SELECT * FROM contents" )
articles = cursor.fetchall()
cursor.close()

cursor = conn.cursor()
cursor.execute( "SELECT * FROM feedback" )
feedback = cursor.fetchall()
cursor.close()

conn.close()


for a in articles :
    # create a fresh entry instance
    
    print a[2]
    
    e = Entry()
    
    e.headline = a[2]
    e.pub_date = a[8]
    
    # We assume you only have one user
    e.user_id = 1
    
    # use the permalink string as the slug, or generate it
    # from the title
    if a[11] == None :
        e.slug = '-'.join(
                     string.translate( a[2],
                                       string.maketrans('',''),
                                       string.punctuation
                                     ).lower().split() )
    else :
        e.slug = a[11]
    
    print "   :: " + e.slug
    
    # if you used any Typo-specific HTML, now would be a good time to
    # strip it out.
    e.body = string.replace( a[4], 'typo:code', 'pre' )
    
    # we have to save the entry here before we continue; adding
    # the tags requires a pk.
    e.save()
    
    # handle the article tags, if there are any
    if a[7] :
        tagstrings = string.translate( a[7], 
                                       string.maketrans('',''), 
                                       string.punctuation
                                     ).lower().split()
        # if there are any new tags, create them. 
        # attach tags to the Entry object
        for t in tagstrings :
            if not Tag.objects.filter( tag = t ) :
                T = Tag()
                T.tag = t
                T.save()
                print "   ++ " + t
        
            e.tags.add( Tag.objects.filter( tag = t )[0] )    
            print "   .. ", t    
    
    e.save()
    
    # create and attach the comments for this Entry
    for f in feedback :
        if a[0] == f[12] :
            
            print "   -> Comment from " + f[3]
            c = Comment()
            c.person_name   = f[3]
            c.person_email  = f[13]
            c.person_www    = f[14]
            c.ip_address    = f[15]
            c.comment       = f[4]
            
            # You should only have one Site instance. If you've
            # got more than one, it's up to you to figure out what
            # to do with the site_id.
            c.site_id = 1
            
            # We'll assume all comments are on Entry objects.
            c.content_type_id = 9
            
            # set the comment to active
            c.is_public = True
            
            # attach the comment to the Entry we just created
            c.object_id = e.id
                        
            c.save()
            
            c.submit_date   = f[6]
            c.save()

f = open( 'typo_articles.pickle', 'w' )
pickle.dump( articles, f )
f.close()

f = open( 'typo_comments.pickle', 'w' )
pickle.dump( feedback, f )
f.close()

